
#include "Stdafx.h"
#include <DxErr.h>

namespace GRIP
{
	namespace Cuda
	{
		// NoCudaDevicesException
		NoCudaDevicesException::NoCudaDevicesException()
			: Exception("No CUDA devices are present on the system.") { };

		NoCudaDevicesException::~NoCudaDevicesException() { };

		// UnsupportedComputeVersionException
		UnsupportedComputeVersionException::UnsupportedComputeVersionException(int major, int minor)
			: Exception("Unsupported compute version: " + major + "." + minor)
		{
			version = major + "." + minor;
		};

		UnsupportedComputeVersionException::~UnsupportedComputeVersionException() { };
		
		String^ UnsupportedComputeVersionException::Version()
		{
			return version;
		};

		// CudaException
		CudaException::CudaException(cudaError_t error, String^ message)
			: Exception(message + GetErrorMessage(error))
		{
			this->error = error;
		};
		
		CudaException::CudaException(CUresult error, String^ message)
			: Exception(message + GetErrorMessage(error))
		{
			this->error = error;
		};
		
		CudaException::CudaException(int error, String^ message)
			: Exception(message + GetErrorMessage(error))
		{
			this->error = error;
		};

		CudaException::~CudaException() { };

		String^ CudaException::GetErrorMessage(cudaError_t error)
		{
			int errNum = (int)error;
			String^ errMsg = gcnew String(cudaGetErrorString(error));
			return " Error Code: " + errNum + " (" + errMsg + ")";
		};

		String^ CudaException::GetErrorMessage(CUresult error)
		{
			int errNum = (int)error;
			String^ errMsg = gcnew String(getCudaDrvErrorString(error));
			return " Error Code: " + errNum + " (" + errMsg + ")";
		};

		String^ CudaException::GetErrorMessage(int error)
		{
			_com_error errObj(error);
			String^ errorMsg = gcnew String(errObj.ErrorMessage());
			return " Error Code: " + error + " (" + errorMsg + ")";
		};

		const int CudaException::Error()
		{
			return error;
		};

		// DirectXException
		DirectXException::DirectXException(HRESULT result, String^ message)
			: Exception(message + GetErrorMessage(result))
		{
			this->error = result;
		};

		DirectXException::~DirectXException() { };

		String^ DirectXException::GetErrorMessage(HRESULT result)
		{
			String^ errMsg = gcnew String(DXGetErrorString(result)) + " - " + gcnew String(DXGetErrorDescription(result));
			return " Error Code: " + result + " (" + errMsg + ")";
		};

		const HRESULT DirectXException::Error()
		{
			return error;
		};
	};
};